package PuriON;

/*
 * PURION_Consumer.java
 *
 * Created on 2008년 5월 23일 (금), 오전 7:31
 */

import PuriON_Chat.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author  hyukmini
 */
public class PURION_Consumer extends javax.swing.JInternalFrame {
    
    private PURION_Player video;
    private PURION_Player audio;
    
    private PURION_Chat_Client chater;
    private String ipAddress = null;
    private String roomTitle = null;
    private Thread t;
    
    private PURION_Main parent = null;
    
    /** Creates new form PURION_Consumer */
    public PURION_Consumer(PURION_Main _p,String _i, String _t) {
        super(_t);        
        initComponents();
        
        parent = _p;
        
        roomTitle = _t;
        ipAddress = _i;
        
        setLocation(30, 30);
        setVisible(true);
        
        initConnect();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        butMsgSend = new javax.swing.JButton();

        setClosable(true);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Chat.png"))); // NOI18N
        jLabel4.setBounds(10, 10, 300, 30);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jScrollPane3.setViewportView(jTextArea1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        butMsgSend.setText("보내기");
        butMsgSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butMsgSendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 338, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(15, 15, 15))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                        .addComponent(butMsgSend)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(butMsgSend)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    public void initConnect() {
            //this.sendQuery();
        
        chater = new PURION_Chat_Client(ipAddress, jTextArea1);
        t = new Thread(chater);
        t.start();
        
        
        video = new PURION_Player("rtp://" +jTextField1.getText() + ":30000/video", true);
        Thread t1 = new Thread(video);
        t1.start();
        audio = new PURION_Player("rtp://" +jTextField1.getText() + ":30002/audio", false);
        Thread t2 = new Thread(audio);
        t2.start();
        
        parent.addComopnent(video);
        parent.addComopnent(audio);
        
        
       
        //채팅 모듈을 생성하고 쓰레드로 돌린다.

        // 우측상단의 창에 현재 접속중인 서버 표시
        this.jLabel4.setText("Connected in " + roomTitle + "(" + ipAddress + ")" ); 
}
    
    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER && !jTextField1.getText().equals("")) {
            chater.messageSend(ipAddress + " : " + jTextField1.getText());
            jTextField1.setText("");
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    private void butMsgSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butMsgSendActionPerformed
        // TODO add your handling code here:
        if (!jTextField1.getText().equals("")) {
            try {
                chater.messageSend(InetAddress.getLocalHost().getHostAddress() + " : " + jTextField1.getText());
                jTextField1.setText("");
            } catch (UnknownHostException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_butMsgSendActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged
        // TODO add your handling code here:

    }//GEN-LAST:event_formMouseDragged

    private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved
        // TODO add your handling code here:
        video.setLocation(this.getLocation().x + this.getWidth(), this.getLocation().y);
    }//GEN-LAST:event_formComponentMoved
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butMsgSend;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    
}
